/*====================================================================================
# Module of «ДЭНСИ: КАССА» complex is the front-office application for POS-systems based on fiscal registrators.
# © 2010, Kostuchenko Alexey (leo-dead@mail.ru).
# This is a FREE software, licensed under GNU/GPL terms. See details in LICENSING_ENG.TXT in main directory
#====================================================================================
# Модуль программного комплекса «ДЭНСИ: КАССА» - рабочее место кассира для POS-систем на базе фискальных регистраторов
# © 2010, Костюченко Алексей (leo-dead@mail.ru).
# Данная программа является свободным программным обеспечением, выпускается на условиях Стандартной Общественной Лицензии GNU/GPL
# Подробнее смотрите в файле LICENSING_RUS.TXT в основной директории
#====================================================================================*/

// TODO при добавлении нового элемента / изменении старого проверять operator_id, internal_id (не повторяется)

void cpBarcodesForm::init()
{
    BarcodesTable->setColumnWidth(0, 100);
    BarcodesTable->setColumnWidth(1, 200);
    BarcodesTable->setColumnWidth(2, 200);
    BarcodesTable->setColumnWidth(3, 200);
}


void cpBarcodesForm::destroy()
{
    
}


int cpBarcodesForm::Load( QString scheme_name )
{
    SetupCyberplat cyberplat_db(LINCASH_CONF_PATH);
    if ( cyberplat_db.er_con != 0 ) {
        return -1;
    }
    
    QTextCodec* codec_db =  QTextCodec::codecForName(DBCODEC);

    if (cyberplat_db.SetScheme(codec_db->fromUnicode(scheme_name)) != 0) {
        return -1;
    }
    
    TCyberplatBarcode* codes = NULL;
    int count_codes;

    if (cyberplat_db.GetBarcodes(codes, count_codes) != 0) {
        return -1;
    }
    
    BarcodesTable->setNumRows(count_codes);
    
    for ( int i = 0; i < count_codes; i++ )   {
        BarcodesTable->setText(i, 0, codec_db->toUnicode(codes[i].operator_id));
        BarcodesTable->setText(i, 1, codec_db->toUnicode(codes[i].internal_id));
        BarcodesTable->setText(i, 2, codec_db->toUnicode(codes[i].pattern));
        BarcodesTable->setText(i, 3, codec_db->toUnicode(codes[i].replacement));
    }
    
    free(codes);
    
    return 0;
}


int cpBarcodesForm::Save( QString scheme_name )
{
    SetupCyberplat cyberplat_db(LINCASH_CONF_PATH);
    if ( cyberplat_db.er_con != 0 ) {
        return -1;
    }
    
    QTextCodec* codec_db =  QTextCodec::codecForName(DBCODEC);

    if (cyberplat_db.SetScheme(codec_db->fromUnicode(scheme_name)) != 0) {
        return -1;
    }
     
    if (cyberplat_db.DeleteBarcodes() != 0) {
        return -1;
    }
            
    int count_codes = BarcodesTable->numRows();
    TCyberplatBarcode code;
    
    for (int i = 0; i < count_codes; i++ ) {
        strcpy(code.operator_id, codec_db->fromUnicode(BarcodesTable->text(i,0)));
        strcpy(code.internal_id, codec_db->fromUnicode(BarcodesTable->text(i,1)));
        strcpy(code.pattern, codec_db->fromUnicode(BarcodesTable->text(i,2)));
        strcpy(code.replacement, codec_db->fromUnicode(BarcodesTable->text(i,3)));
        cyberplat_db.AddBarcode(code);
    }

    return 0;
}

void cpBarcodesForm::BarcodesTable_CurrentChanged( int row, int )
{
    if (row < 0) {
        EditButton->setEnabled(false);
        RemoveButton->setEnabled(false);
        return;
    }
    
    EditButton->setEnabled(true);
    RemoveButton->setEnabled(true);
    
    OperatorEdit->setText(BarcodesTable->text(row, 0));
    InternalEdit->setText(BarcodesTable->text(row, 1));
    PatternEdit->setText(BarcodesTable->text(row, 2));
    ReplacementEdit->setText(BarcodesTable->text(row, 3));
}


void cpBarcodesForm::AddButton_Clicked()
{
    QString operator_id = OperatorEdit->text();
    QString internal_id = InternalEdit->text();
    QString pattern = PatternEdit->text();
    QString replacement = ReplacementEdit->text();
    
    int row = BarcodesTable->numRows();
    BarcodesTable->setNumRows(row+1);
    
    BarcodesTable->setText(row, 0, operator_id);
    BarcodesTable->setText(row, 1, internal_id);
    BarcodesTable->setText(row, 2, pattern);
    BarcodesTable->setText(row, 3, replacement);
    
    BarcodesTable->setCurrentCell(row, 0);
    
    emit DataChanged();
}


void cpBarcodesForm::RemoveButton_Clicked()
{
    int row = BarcodesTable->currentRow();
    if (row == -1) return;
    BarcodesTable->removeRow(row);
    if (row>BarcodesTable->numRows()) row--;
    BarcodesTable->setCurrentCell(row, 0);
    
    emit DataChanged();
}


void cpBarcodesForm::EditButton_Clicked()
{
    int row = BarcodesTable->currentRow();
    if (row == -1) return;
    
    QString operator_id = OperatorEdit->text();
    QString internal_id = InternalEdit->text();
    QString pattern = PatternEdit->text();
    QString replacement = ReplacementEdit->text();
    
    BarcodesTable->setText(row, 0, operator_id);
    BarcodesTable->setText(row, 1, internal_id);
    BarcodesTable->setText(row, 2, pattern);
    BarcodesTable->setText(row, 3, replacement);
    
    emit DataChanged();
}

